﻿// <copyright file="MockUserProvider.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the mock user provider</summary>

namespace Sqwarea.Persistence.Mock
{
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Provides a user instance
    /// </summary>
    public class MockUserProvider : IUserProvider
    {
        /// <summary>
        /// Users dictionnary
        /// </summary>
        private static Dictionary<string, User> users = new Dictionary<string, User>();

        /// <summary>
        /// returns the user, given its OpenId.
        /// </summary>
        /// <param name="openId">The openId you want the user</param>
        /// <returns>The user you gave the openid.</returns>
        public User GetUserByOpenId(string openId)
        {
            var user = MockUserProvider.users[openId];
            if (user.RemembranceSessions == null)
            {
                user.RemembranceSessions = new Dictionary<string, RemembranceSession>();
            }

            return user;
        }

        /// <summary>
        /// Returns a list of users of the given openIds.
        /// </summary>
        /// <param name="openIds">The list of openId</param>
        /// <returns>The list of users</returns>
        public IEnumerable<User> GetUsersByOpenId(IEnumerable<string> openIds)
        {
            return openIds.Select(openId => GetUserByOpenId(openId));
        }

        /// <summary>
        /// returns whether or not the user exists
        /// </summary>
        /// <param name="openId">The openId</param>
        /// <returns>Whether or not the user exists</returns>
        public bool UserExists(string openId)
        {
            return MockUserProvider.users.ContainsKey(openId);
        }

        /// <summary>
        /// Delete a user.
        /// </summary>
        /// <param name="user">The user to delete</param>
        public void DeleteUser(User user)
        {
            MockUserProvider.users.Remove(user.OpenId);
        }

        /// <summary>
        /// Saves the user on the cloud.
        /// </summary>
        /// <param name="user">The user you want to update parameters.</param>
        public void UpdateUser(User user)
        {
            user.Id = 1;
            MockUserProvider.users[user.OpenId] = user;
        }

        // Clear all

        /// <summary>
        /// Clear all data (used only for tests purpose)
        /// </summary>
        internal void ClearAll()
        {
            users.Clear();
        }
    }
}
